package com.jmp.security.grant;

import com.jmp.security.base.ClientDetail;
import com.jmp.security.base.UserDetail;
import com.jmp.security.base.UserDetailService;
import one.stand.advice.ResultEnum;
import one.stand.model.User;
import one.stand.service.UserService;
import one.stand.util.AssertUtil;
import org.apache.commons.lang3.StringUtils;

public class PasswordUserDetailServiceImpl<T extends PasswordRequestDetail> implements UserDetailService<T> {
    private UserService userService;

    public PasswordUserDetailServiceImpl(UserService userService) {
        this.userService = userService;
    }

    @Override
    public UserDetail get(T requestDetail, ClientDetail clientDetail) {
        String username = StringUtils.isNoneBlank(requestDetail.getUsername()) ? StringUtils.trim(requestDetail.getUsername()) : null;
        String password = StringUtils.isNoneBlank(requestDetail.getPassword()) ? StringUtils.trim(requestDetail.getPassword()) : null;

        User userModel = userService.getByPasswd(username, password);
        AssertUtil.nonNull(userModel, ResultEnum.PARAM_CHECK, "用户名或密码错误");

        UserDetail userDetail = new UserDetail();
        userDetail.setUserModel(userModel);
        return userDetail;
    }

}
